package com.quick.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.quick.scan.impl.OnResultListerner;
import com.quick.scan.view.ZXingView;

public class ScanActivity extends Activity implements View.OnClickListener {

    private ZXingView mZXingView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_capture);
        mZXingView = (ZXingView) findViewById(R.id.zxingview);
        mZXingView.setOnResultListerner(new OnResultListerner() {

            @Override
            public void onResult(String result) {
                onResultHandler(result);
            }

        });
        Button mButtonBack = (Button) findViewById(R.id.button_back);
        mButtonBack.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mZXingView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mZXingView.onPause();
    }

    @Override
    protected void onDestroy() {
        mZXingView.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_back:
                this.finish();
                break;
        }
    }

    public void onResultHandler(String resultString) {
        if (TextUtils.isEmpty(resultString)) {
            Toast.makeText(ScanActivity.this, "Scan failed!",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        Intent resultIntent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("result", resultString);
        resultIntent.putExtras(bundle);
        this.setResult(RESULT_OK, resultIntent);
        ScanActivity.this.finish();
    }

}